package test.mt;

import java.util.Scanner;

/**
 * @Description
 * @Author Firenut
 * @Date 2023-08-19 19:05
 */
public class T1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int q = in.nextInt();
            int i = 0;
            while (i++ < q) {
                long m = in.nextLong();
                long x = in.nextLong();
                if (x % m == 0) {
                    System.out.println(m);
                    continue;
                }
                long ans = x % m == 0 ? 1 : x % m;
                System.out.println(ans);
            }
        }
    }
    //4
    //2 3
    //5 17
    //8 2
    //4 4

    //1
    //2
    //2
    //4
}
